package com.yidu.ladder.delivery.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.ladder.delivery.dao.PackDao;
import com.yidu.ladder.delivery.domain.BasicJobList;
import com.yidu.ladder.delivery.domain.ChargingInfo;
import com.yidu.ladder.delivery.domain.Pack;
import com.yidu.ladder.delivery.domain.PackInfo;
import com.yidu.ladder.delivery.domain.PackVo;
import com.yidu.ladder.delivery.service.PackService;
import com.yidu.ladder.util.IDGenerator;

/**
 * 包装业务逻辑实现类
 * @author ld
 *
 */
@Service
public class PackServiceImpl implements PackService {
	
	@Autowired
	private PackDao packDao;

	@Override
	public List<String> findAllBasicJobListNo() {
		//调用查询所有基本工作单编号
		return packDao.findAllBasicJobListNo();
	}

	@Override
	public BasicJobList findBasicJobList(String jobListNo) {
		//调用根据工作单编号查询基本工作单
		return packDao.findBasicJobListByJobListNo(jobListNo);
	}

	@Override
	public boolean addPack(List<Pack> packList) {
		//遍历包装集合
		for (Pack p : packList) {
			//生成包装单ID
			p.setPackageId(IDGenerator.getId());
			//操作时间
			p.setOperateTime(new Date());
		}
		//调用添加包装方法
		return packDao.addPack(packList) == packList.size();
	}

	@Override
	public Map<String, Object> findAllPack(Map<String, Object> paramMap) {
		//获取当前页码
		String pageStr = (String) paramMap.get("page");
		//获取每页显示数量
		String limitStr = (String) paramMap.get("limit");
		//转为Integer类型
		Integer page = Integer.valueOf(pageStr);
		Integer limit = Integer.valueOf(limitStr);
		//计算分页开始索引
		paramMap.put("page", (page - 1) * limit);
		//每页显示数量
		paramMap.put("limit", limit);
		//调用查询所有包装单
		List<Pack> packList = packDao.findAllPack(paramMap);
		//查询包装单数量
		int total = packDao.findPackCount(paramMap);
		//定义map,返回给前端的数据格式
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", packList);
		map.put("count", total);
		return map;
	}

	@Override
	public Map<String, Object> findAllPackInfo() {
		//查询所有包装信息
		List<PackInfo> packList = packDao.findAllPackInfo();
		//查询包装信息数量
		int total = packDao.findPackInfoCount();
		//定义map,返回给前端的数据格式
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", packList);
		map.put("count", total);
		return map;
	}

	@Override
	public List<String> findAllPackJobListNo() {
		//调用查询所有包装单的工作单号
		return packDao.findAllPackJobListNo();
	}

	@Override
	public Pack findPack(String jobListNo) {
		//调用查询包装单
		return packDao.findPackByJobListNo(jobListNo);
	}

	@Override
	public PackInfo findPackInfoByProjectAndSize(String packaingProject, String specifications) {
		//创建一个Map集合用来存储数据
		Map<String,Object> paramMap = new HashMap<String, Object>();
		//将服务费存入Map集合中
		paramMap.put("packaingProject", packaingProject);
		//将材料费存入Map集合中
		paramMap.put("specifications", specifications);
		//调用查询包装信息（包装费）
		return packDao.findPackInfoByProjectAndSize(paramMap);
	}

	@Override
	public boolean updatePackInfo(Pack pack, BasicJobList basicJobList, ChargingInfo chargingInfo) {
		//入库时间
		pack.setStorageTime(new Date());
		//定义一个int型数据接收修改包装表信息
		int result1 = packDao.updatePack(pack);
		//定义一个int型数据接收修改工作单表信息
		int result2 = packDao.updateBasicJoblist(basicJobList);
		//获得工作单表的工作单号
		String jobListNo = basicJobList.getJobListNo();
		//根据工作单号查询基本工作单表
		basicJobList = packDao.findBasicJobListByJobListNo(jobListNo);
		//为计费信息表的工作单Id赋值
		chargingInfo.setJobListId(basicJobList.getJobListId());
		//定义一个int型数据接收修改计费信息表信息
		int result3 = packDao.updateChargingInfo(chargingInfo);
		//判断无论是否修改都显示修改成功
		if(result1 == 1 && result2 == 1 && result3 == 1) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public Map<String, Object> findPackWhole(Map<String, Object> paramMap) {
		//获取当前页码
		String pageStr = (String) paramMap.get("page");
		//获取每页显示数量
		String limitStr = (String) paramMap.get("limit");
		//转为Integer类型
		Integer page = Integer.valueOf(pageStr);
		Integer limit = Integer.valueOf(limitStr);
		//计算分页开始索引
		paramMap.put("page", (page - 1) * limit);
		//每页显示数量
		paramMap.put("limit", limit);
		//调用查询完整包装信息
		List<PackVo> packVoList = packDao.findPackWhole(paramMap);
		//调用查询完整包装数量
		int total = packDao.findPackWholeCount(paramMap);
		//定义map,返回给前端的数据格式
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", packVoList);
		map.put("count", total);
		return map;
	}

	@Override
	public PackVo findBarCodeInfo(String jobListNo) {
		//调用查询条码信息
		return packDao.findBarCodeInfo(jobListNo);
	}

	@Override
	public List<String> findAllPackSuccess() {
		//调用查询所有打包完的工作单号
		return packDao.findAllPackSuccess();
	}

	
}
